package filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import dao.CheckSellUserInfo;

/**
 * Servlet Filter implementation class SellCheck
 * 检查当前用户信息完整性，若不完整则跳到用户信息修改界面，若完整则可以发布新商品
 */
public class SellCheck implements Filter {

    /**
     * Default constructor. 
     */
    public SellCheck() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// place your code here
		
		HttpServletRequest httpServletRequest = (HttpServletRequest) request;
		HttpSession session = httpServletRequest.getSession();
		String numString = (String) session.getAttribute("usernum");//取得session中得字段信息
		if(!CheckSellUserInfo.checkInfo(numString)){//检查用户信息是否完整
			request.getRequestDispatcher("/user/update.jsp").forward(request, response);//若不完，转入用户信息修改界面
			return;
		}

		// pass the request along the filter chain
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	@Override
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
